//
//  HHPHTTPRequestManager.m
//  HHPHTTPRequest
//
//  Created by 韩红坡 on 2016/12/14.
//  Copyright © 2016年 韩红坡. All rights reserved.
//

#import "HHPHTTPRequestManager.h"

@interface HHPHTTPRequest (Access)

@property (nonatomic, strong) AFHTTPSessionManager *sessionManager;
@property (nonatomic, strong) HHPHTTPRequestOptions *options;

@end

@interface HHPHTTPRequestManager ()

@property (nonatomic, strong) HHPHTTPRequestOptions *defaultOptions;

@end

@implementation HHPHTTPRequestManager

+ (instancetype)manager
{
    return [self managerWithName:@"_default"];
}

+ (instancetype)managerWithName:(NSString *)name
{
    static NSMutableDictionary *requestManagers;
    if (!requestManagers) {
        requestManagers = [NSMutableDictionary dictionary];
    }
    
    HHPHTTPRequestManager *requestManager = requestManagers[name];
    if (!requestManager) {
        requestManager = [[HHPHTTPRequestManager alloc] init];
        requestManagers[name] = requestManager;
    }
    
    return requestManager;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.defaultOptions = [[HHPHTTPRequestOptions alloc] init];
    }
    
    return self;
}

- (HHPHTTPRequest *)createRequest
{
    HHPHTTPRequest *request = [[HHPHTTPRequest alloc] initWithSessionManager:self.sessionManager];
    if (self.defaultOptionsBlock) {
        self.defaultOptionsBlock(request.options);
    }
    
    return request;
}

@end
